<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 表单区 -->
      <el-form class="login_form" :rules="loginrules" :model="loginform" label-width="0px" ref="loginref">
        <el-form-item prop="username">
          <el-input v-model="loginform.username" prefix-icon="el-icon-s-custom"></el-input>
        </el-form-item>
         <el-form-item prop="password">
          <el-input prefix-icon="el-icon-lock" v-model="loginform.password" type="password"></el-input>
        </el-form-item>
        <!-- 按钮区 -->
         <el-form-item class="btns">
          <el-button type="primary" @click="login">登陆</el-button>
          <el-button type="info" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      loginform: {
        username: 'admin',
        password: '123456'
      },
      //   表单验证规则
      loginrules: {
        username: [
          { min: 3, max: 10, required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [{ min: 3, max: 10, required: true, message: '请输入密码', trigger: 'blur' }]

      }
    }
  },
  methods: {
    // 点击重置按钮 绑定事件
    resetForm () {
    //   console.log(this)
      this.$refs.loginref.resetFields()
    },
    login () {
      this.$refs.loginref.validate(async valid => {
        // console.log(valid)
        if (!valid) return
        // 打印返回的数据
        // const result = this.$http.post('login', this.loginform)
        // console.log(result)
        // 直接解构返回结果中的data 遇到promise 使用await 必须是在async的修饰函数中
        const { data: res } = await this.$http.post('login', this.loginform)
        // console.log(res)
        if (res.meta.status !== 200) return this.$message.error('登陆失败')
        this.$message.success('登陆成功')
        // 将登陆之后的token保存至sessionStorage中，因为是会话形式，如果是持久化应该保存至cookiesstorage,token只在网站打开时生效
        window.sessionStorage.setItem('token', res.data.token)
        // 通过编程式导航跳转到后台主页，路由地址为 /home
        this.$router.push('/home')
      })
    }
  }
}
</script>
<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.avatar_box {
  height: 130px;
  width: 130px;
  border: 1px solid rgb(136, 201, 32);
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
  }
}
.btns{
    display: flex;
    justify-content: flex-end;
}
.login_form{
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
}
</style>
